package com.lemon.smartsafetynew.ui.activity.record;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lemon.android.http.HttpUtils;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafety.utils.common.LemonFileHelper;
import com.lemon.smartsafetynew.entity.FileInfo;
import com.lemon.smartsafetynew.entity.event.incident.MediaEvent;
import com.lemon.smartsafetynew.entity.patrol.SafePatrolMediaVo;
import com.lemon.smartsafetynew.entity.record.TankVo;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafetynew.ui.adapter.incident.IncidentAttachmentAdapter;
import com.lemon.smartsafetynew.ui.adapter.record.FilesAdapter;
import com.lemon.smartsafetynew.util.common.CommonUtil;
import com.lemon.smartsafetynew.util.constants.KeyConstants;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.finalteam.galleryfinal.model.PhotoInfo;

/**
 * 储罐信息详情
 */
public class RecordTankDetailActivity extends SwipeBackActivity implements RecyclerTClickListener<FileInfo> {
    @Bind(R.id.iv_return)
    ImageView ivReturn;
    @Bind(R.id.record_tank_name_tv)
    TextView recordTankNameTv;
    @Bind(R.id.record_tank_material_name2_tv)
    TextView recordTankMaterialName2Tv;
    @Bind(R.id.record_tank_num_tv)
    TextView recordTankNumTv;
    @Bind(R.id.record_tank_type_tv)
    TextView recordTankTypeTv;
    @Bind(R.id.record_tank_texture_tv)
    TextView recordTankTextureTv;
    @Bind(R.id.record_tank_width_tv)
    TextView recordTankWidthTv;
    @Bind(R.id.record_tank_height_tv)
    TextView recordTankHeightTv;
    @Bind(R.id.record_tank_volume_tv)
    TextView recordTankVolumeTv;
    @Bind(R.id.record_fire_danger_level_tv)
    TextView recordFireDangerLevelTv;
    @Bind(R.id.record_tank_cas_num_tv)
    TextView recordTankCasNumTv;
    @Bind(R.id.record_fire_dike_tv)
    TextView recordFireDikeTv;
    @Bind(R.id.record_fire_dike_area_tv)
    TextView recordFireDikeAreaTv;
    @Bind(R.id.record_tank_remark_tv)
    TextView recordTankRemarkTv;
    @Bind(R.id.ryMaterial)
    RecyclerView ryMaterial;
    @Bind(R.id.ryMaterialFile)
    RecyclerView ryMaterialFile;
    @Bind(R.id.tv_file_none)
    TextView tvFileNone;
    @Bind(R.id.danger_type_tv)
    TextView dangerTypeTv;
    @Bind(R.id.record_tank_area_volume_tv)
    TextView recordTankAreaVolumeTv;

    private Boolean isShowReturn = false;
    private IncidentAttachmentAdapter mMediaAdapter;
    private static TankVo data;
    @Override
    protected void getBundleArgs() {
        super.getBundleArgs();

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            if (bundle.containsKey(KeyConstants.KEY_TANK_VO)) {
                data = (TankVo) bundle.getSerializable(KeyConstants.KEY_TANK_VO);
                isShowReturn = bundle.getBoolean("isShowReturn");
            }
        }
    }

    @Override
    protected void initTitle() {
        this.setBarTitle(getString(R.string.record_tanks_name));
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(RecordTankDetailActivity.this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_record_tank_detail;
    }

    @Override
    protected void initViews() {
        super.initViews();

        if (isShowReturn) {
            ivReturn.setVisibility(View.VISIBLE);
        } else {
            ivReturn.setVisibility(View.GONE);
        }
        if (data != null) {
            dangerTypeTv.setText(CommonUtil.avoidNullString(data.getDangerousChemicals()));
            recordTankNameTv.setText(CommonUtil.avoidNullString(data.getName()));
            recordTankMaterialName2Tv.setText(CommonUtil.avoidNullString(data.getMaterialName()));
            recordTankNumTv.setText(CommonUtil.avoidNullString(data.getOrder()));
            recordTankTypeTv.setText(CommonUtil.avoidNullString(data.getType()));
            recordTankTextureTv.setText(CommonUtil.avoidNullString(data.getMaterials()));
            recordTankWidthTv.setText(CommonUtil.avoidNullString(data.getDiameter()));
            recordTankHeightTv.setText(CommonUtil.avoidNullString(data.getHigh()));
            recordTankVolumeTv.setText(CommonUtil.avoidNullStringNoTitle(data.getVolume()));
            recordFireDangerLevelTv.setText(CommonUtil.avoidNullString(data.getFireRating()));
            recordTankCasNumTv.setText(CommonUtil.avoidNullString(data.getCas()));
            recordFireDikeTv.setText(CommonUtil.avoidNullString(data.getFireproofingDyke()));
            recordFireDikeAreaTv.setText(CommonUtil.avoidNullString(data.getDykeSize()));
            recordTankRemarkTv.setText(CommonUtil.avoidNullString(data.getRemark()));
            recordTankAreaVolumeTv.setText(CommonUtil.avoidNullString(data.getSize()));
            initAdapter();
        }
    }

    private void initAdapter() {
        if (!TextUtils.isEmpty(data.getPicture())) {
            List<String> idList = Arrays.asList(data.getPicture().split(","));
            List<SafePatrolMediaVo> photoVo = new ArrayList<>();
            for (int i = 0; i < idList.size(); i++) {
                SafePatrolMediaVo safePatrolMediaVo = new SafePatrolMediaVo();
                safePatrolMediaVo.setData_url(idList.get(i));
                photoVo.add(safePatrolMediaVo);
            }
            GridLayoutManager gridLayoutManager = new GridLayoutManager(RecordTankDetailActivity.this, 3);
            ryMaterial.setLayoutManager(gridLayoutManager);
            ryMaterial.setNestedScrollingEnabled(false);
            mMediaAdapter = new IncidentAttachmentAdapter(photoVo, RecordTankDetailActivity.class.getName());
            ryMaterial.setAdapter(mMediaAdapter);
        }
        if (!TextUtils.isEmpty(data.getFile())) {
            tvFileNone.setVisibility(View.GONE);
            ryMaterialFile.setVisibility(View.VISIBLE);
            List<String> list = Arrays.asList(data.getFile().split(","));//根据逗号分隔转化为list
            List<FileInfo> fileList = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                FileInfo fileinfo = new FileInfo();
                fileinfo.setName("附件一");
                fileinfo.setUrl(HttpUtils.formatFilesUrl(list.get(i)));
                fileList.add(fileinfo);
            }
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(RecordTankDetailActivity.this);
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            ryMaterialFile.setLayoutManager(linearLayoutManager);
            ryMaterialFile.setNestedScrollingEnabled(false);
            FilesAdapter mAdapter = new FilesAdapter(fileList, this);
            ryMaterialFile.setAdapter(mAdapter);
        } else {
            tvFileNone.setVisibility(View.VISIBLE);
            ryMaterialFile.setVisibility(View.GONE);
        }
    }

    /**
     * 显示图片和音频详情
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMediaEvent(MediaEvent event) {

        if (RecordTankDetailActivity.class.getName().equals(event.getTag())) {

            SafePatrolMediaVo data = event.getMediaVos().get(event.getPosition());

            if (LemonFileHelper.isImageUrl(data.getData_url())) {
                List<SafePatrolMediaVo> mediaVos = event.getMediaVos();
                List<PhotoInfo> photos = new ArrayList<>();

                int j = 0;
                int currentItem = 0;

                for (int i = 0; i < mediaVos.size(); i++) {
                    if (LemonFileHelper.isImageUrl(mediaVos.get(i).getData_url())) {
                        if (data.getData_url().equals(mediaVos.get(i).getData_url())) {
                            currentItem = j;
                        }
                        PhotoInfo photoInfo = new PhotoInfo();
                        photoInfo.setPhotoId(j);
                        photoInfo.setPhotoPath(HttpUtils.formatFilesUrl(mediaVos.get(i).getData_url()));
                        photos.add(photoInfo);
                        j++;
                    }
                }

                if (photos != null && photos.size() != 0) {
                    UIHelper.showPhotoView(this, photos, currentItem);
                }
            } else if (LemonFileHelper.isVoiceUrl(data.getData_url())) {
                playVoice(HttpUtils.formatFilesUrl(data.getData_url()));
            }

        }
    }

    @OnClick(R.id.iv_return)
    public void onViewClicked() {
        AppManager.getAppManager().finishActivity(RecordTankDetailActivity.class);
        AppManager.getAppManager().finishActivity(RecordTankActivity.class);
    }

    @Override
    public void onItemClick(FileInfo data, int position) {
        downloadAndShowFile(data.getUrl(), data.getName(), this, getResources().getString(R.string.record_tanks_name));
    }

}
